package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.pojo.TbBrand;
import com.leyou.item.pojo.TbCategory;
import com.leyou.item.service.TbBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: guojy
 * @date: 2020/2/29 19:42
 * @Description: 操作品牌数据
 * @version:
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private TbBrandService tbBrandService;

    /**
     * 分页查询 品牌数据
     *
     * @param key    关键字  不必须
     * @param page   当前页码
     * @param rows   每页显示多少条
     * @param sortBy 排序字段 不必须
     * @param desc   是否降序
     * @return 品牌数据
     */
    @RequestMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc
    ) {
        PageResult<BrandDTO> pageResult = tbBrandService.searchPage(page, rows, key, sortBy, desc);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 保存品牌数据
     *
     * @param tbBrand 品牌基础数据
     * @param cids    所属分类id
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(TbBrand tbBrand,
                                          @RequestParam(name = "cids") List<Long> cids) {
        tbBrandService.saveBrand(tbBrand, cids);

        return ResponseEntity.status(HttpStatus.ACCEPTED).body(null);
    }

    /**
     * 保存品牌数据
     *
     * @param tbBrand 品牌基础数据
     * @param cids    所属分类id
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> editBrand(TbBrand tbBrand,
                                          @RequestParam(name = "cids") List<Long> cids) {
        tbBrandService.updateBrand(tbBrand, cids);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(null);
    }

    /**
     * 删除
     *
     * @param brandId
     * @return
     */
    @GetMapping("/of/delete")
    public ResponseEntity<Void> deleteBrand(Long brandId) {
        tbBrandService.deleteBrand(brandId);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(null);
    }

    /**
     * 新增商品时 根据分类查询所有品牌信息
     *
     * @param categoryId
     * @return
     */
    //http://api.leyou.com/api/item/brand/of/category?id=76
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findBrandByCid(@RequestParam("id") Long categoryId) {
        List<BrandDTO> brandDTOList = tbBrandService.queryByCategoryId(categoryId);
        return ResponseEntity.ok(brandDTOList);
    }


    /**
     * TODO 根据品牌id查询  品牌
     * PathVariable 用于restFul风格 路径接参
     *
     * @param id 品牌id
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable(name = "id") Long id) {
        return ResponseEntity.ok(tbBrandService.findByid(id));
    }

    /**
     * 查询品牌集合
     *
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findBrandsByIds(@RequestParam(name = "ids") List<Long> ids) {
        List<BrandDTO> brandDTOList = tbBrandService.findBrandsByIds(ids);
        return ResponseEntity.ok(brandDTOList);
    }
}
